package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/studentjk")
public class SchoolStudentjkController extends BaseController {
    @Autowired
    private ISchoolStudentjkService schoolStudentjkService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISchoolClassesService schoolClassesService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询学生所属培训列表
     */
    @RequiresPermissions("business:studentjk:list")
    @GetMapping("/list")
    public TableDataInfo list(SchoolStudentjk schoolStudent)
    {
        startPage();
        List<SchoolStudentjk> list = schoolStudentjkService.selectSchoolStudentjkList(schoolStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生所属培训列表
     */
    @RequiresPermissions("business:studentjk:export")
    @Log(title = "学生所属培训", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, SchoolStudentjk schoolStudent)
    {
        List<SchoolStudentjk> list = schoolStudentjkService.selectSchoolStudentjkList(schoolStudent);
        for (SchoolStudentjk obj:list) {
            SysDept sysDept = sysDeptService.selectDeptById(obj.getSchoolId());
            SchoolClasses schoolClasses = schoolClassesService.selectSchoolClassesById(obj.getClassesId());
            SysUser sysUserJs = sysUserService.selectUserById(obj.getTeacherId());
            SysUser sysUserXs = sysUserService.selectUserById(obj.getStudentId());
            obj.setSchoolName(sysDept.getDeptName());
            obj.setClassesName(schoolClasses.getClassesName());
            obj.setXsUsername(sysUserXs.getUserName());
            obj.setXsPhone(sysUserXs.getPhonenumber());
            obj.setOpUsername(sysUserJs.getUserName());
            obj.setOpPhone(sysUserJs.getPhonenumber());
        }
        ExcelUtil<SchoolStudentjk> util = new ExcelUtil<SchoolStudentjk>(SchoolStudentjk.class);
        util.exportExcel(response, list, "学生所属培训");
    }

    /**
     * 获取学生所属培训详细信息
     */
    @RequiresPermissions("business:studentjk:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolStudentjkService.selectSchoolStudentjkById(id));
    }

    /**
     * 新增学生所属培训
     */
    @RequiresPermissions("business:studentjk:add")
    @Log(title = "学生所属培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolStudentjk schoolStudent)
    {
        return toAjax(schoolStudentjkService.insertSchoolStudentjk(schoolStudent));
    }

    /**
     * 修改学生所属培训
     */
    @RequiresPermissions("business:studentjk:edit")
    @Log(title = "学生所属培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolStudentjk schoolStudent)
    {
        return toAjax(schoolStudentjkService.updateSchoolStudentjk(schoolStudent));
    }

    @RequiresPermissions("business:studentjk:editAll")
    @Log(title = "学生培训设置", businessType = BusinessType.UPDATE)
    @PutMapping("/all")
    public AjaxResult editAll(@RequestBody List<SchoolStudentjk> list)
    {
        return toAjax(schoolStudentjkService.updateSchoolStudentjkAll(list));
    }

    /**
     * 删除学生所属培训
     */
    @RequiresPermissions("business:student:remove")
    @Log(title = "学生所属培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolStudentjkService.deleteSchoolStudentjkByIds(ids));
    }
}
